<!--
 * @Description: 
 * @Date: 2024-07-08 09:20:03
 * @LastEditTime: 2024-07-31 11:12:07
-->
<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择网格" prop="gridId">
            <el-tree-select
              v-model="formData.gridId"
              :data="deptList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              @change="handleChange"
              placeholder="请选择归属部门"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择人员" prop="userId">
            <el-select v-model="formData.userId" clearable>
              <el-option
                v-for="form in gridmemberList"
                :key="form.id"
                :label="form.nickname"
                :value="form.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { FormRules } from 'element-plus'
import * as getgridApi from '@/api/countryside/gridmanagement/index'
import * as ReportApi from '@/api/countryside/report'
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'
import * as UserApi from '@/api/system/user'
import { formatTime } from '@/utils/index'
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
const deptList = ref<Tree[]>([]) // 树形结构
const reportId = ref(null)
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('转交事件') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  gridId: '',
  userId: ''
})
const emit = defineEmits(['success'])
const gridList = ref<any[]>([])
const gridmemberList = ref<any[]>([])
const formRules = reactive<FormRules>({
  gridId: [{ required: true, message: '选择网格不能为空', trigger: 'change' }],
  userId: [{ required: true, message: '选择人员不能为空', trigger: 'change' }]
})
const handleChange = async (value) => {
  const data = await UserApi.getUserPage({
    deptId: formData.value.gridId
  })
  gridmemberList.value = data.list
}
const formRef = ref()
/** 打开弹窗 */
const open = async (id) => {
  reportId.value = id
  dialogVisible.value = true
}
onMounted(async () => {
  deptList.value = handleTree(await DeptApi.getDeptPage({ pageNo: 1, pageSize: 100, name: '网格' }))
})

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const submitForm = async () => {
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  await ReportApi.updateReport({
    id: Number(reportId.value),
    gridId: Number(formData.value.gridId),
    userId: Number(formData.value.userId),
    transTime: userStore.user.nickname + ',' + formatTime(new Date(), 'yyyy-MM-dd HH:mm:ss'),
    status: 2
  })
  dialogVisible.value = false
  //发送操作成功的事件
  emit('success', true)
}
</script>
<style lang="scss" scoped></style>
